package cn.bmbanjia.zebra2024.apps.meituanMiniApp.user.controller;

import cn.bmbanjia.zebra2024.apps.base.verificationCode.service.IVerificationCodeService;
import cn.bmbanjia.zebra2024.apps.meituanMiniApp.satoken.MeituanCurrentUser;
import cn.bmbanjia.zebra2024.apps.meituanMiniApp.satoken.StpMeituanUtil;
import cn.bmbanjia.zebra2024.apps.meituanMiniApp.user.req.BindMobileReq;
import cn.bmbanjia.zebra2024.apps.meituanMiniApp.user.req.CodeReq;
import cn.bmbanjia.zebra2024.apps.meituanMiniApp.user.req.LoginReq;
import cn.bmbanjia.zebra2024.apps.meituanMiniApp.user.resp.MeituanCurrentUserResp;
import cn.bmbanjia.zebra2024.frame.resp.Resp;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import io.github.linpeilie.Converter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import cn.bmbanjia.zebra2024.apps.meituanMiniApp.user.service.IMeituanMiniAppUserService;

/**
 * 美团小程序用户 控制层。
 *
 * @author fugui
 * @since 2024-06-15
 */
@RestController
@RequestMapping("/meituanMiniApp/user")
@Tag(name = "美团小程序用户")
public class MeituanMiniAppUserController {

    @Resource
    private Converter converter;
    @Resource
    private IMeituanMiniAppUserService meituanMiniAppUserService;
    @Resource
    private IVerificationCodeService verificationCodeService;

    @Operation(summary = "解绑手机号")
    @PostMapping("/unbindMobile")
    public Resp<Void> unbindMobile() {
        return meituanMiniAppUserService.unbindMobile();
    }

    @Operation(summary = "绑定手机号")
    @PostMapping("/bindMobile")
    public Resp<Void> bindMobile(@RequestBody @Valid BindMobileReq req) {
        return meituanMiniAppUserService.bindMobile(req);
    }

    @Operation(summary = "发送验证码")
    @PostMapping("/sendCode")
    public Resp<Void> sendCode(@RequestBody @Valid CodeReq req) {
        return verificationCodeService.sendCode(req);
    }

    @Operation(summary = "注销登录")
    @PostMapping("/logout")
    public Resp<Void> logout() {
        StpMeituanUtil.logout();
        return Resp.success();
    }

    @Operation(summary = "当前用户")
    @PostMapping("/current")
    public Resp<Void> current() {
        MeituanCurrentUser meituanCurrentUser = StpMeituanUtil.currentUser();
        MeituanCurrentUserResp resp = converter.convert(meituanCurrentUser, MeituanCurrentUserResp.class);
        if (StrUtil.isNotBlank(resp.getUserMobile())) {
            resp.setUserMobile(PhoneUtil.hideBetween(resp.getUserMobile()).toString());
        }
        return Resp.success();
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public Resp<Void> login(@RequestBody @Valid LoginReq req) {
        return meituanMiniAppUserService.login(req);
    }

}
